import Duck from 'extensible-duck'
import produce from 'immer'

export default new Duck({
  namespace: 'admin',
  store: 'project',
  types: ['SET_ACTIVE_TAB', 'SET_TASKS_ACTIVE_TAB', 'SET_CONFIG_ACTIVE_TAB'],
  initialState: {
    activeTab: '',
    tasks: {
      activeTab: '',
    },
    config: {
      activeTab: '',
    },
  },
  reducer: produce((draft, action, duck) => {
    switch (action.type) {
      case duck.types.SET_ACTIVE_TAB:
        draft.activeTab = action.tab
        break
      case duck.types.SET_TASKS_ACTIVE_TAB:
        draft.tasks.activeTab = action.tab
        break
      case duck.types.SET_CONFIG_ACTIVE_TAB:
        draft.config.activeTab = action.tab
        break
      default:
        break
    }
  }),
  selectors: {
    activeTab: state => state.project.activeTab,
    activeTaskTab: state => state.project.tasks.activeTab,
    activeConfigTab: state => state.project.config.activeTab,
  },
  creators: duck => ({
    actionSetActiveTab: tab => ({ type: duck.types.SET_ACTIVE_TAB, tab }),
    actionSetActiveTaskTab: tab => ({ type: duck.types.SET_TASKS_ACTIVE_TAB, tab }),
    actionSetActiveConfigTab: tab => ({ type: duck.types.SET_CONFIG_ACTIVE_TAB, tab }),
  }),
})
